package clases.academico;


import CapaDatos.DataSource;
import CapaDatos.Sql;
import clases.incio.Paginacion;
import global.Dialogos;
import javax.swing.JComboBox;

public class Area {

    private int idarea=0;
    private String area;
    private final DataSource dt;
    public Paginacion objPaginacion;
    private String sql;

    public Area() {
        dt = new DataSource();
        objPaginacion = new Paginacion();
    }

    public int getIdarea() {
        return idarea;
    }

    public void setIdarea(int idarea) {
        this.idarea = idarea;
    }

    public String getArea() {
        return area;
    }

    public void setArea(String area) {
        this.area = area;
    }

    private int getMax() {
        return Sql.getMax("IDAREA", "AREA", true);
    }

    public boolean guardarArea() {
        if ("".equals(area)) {
            Dialogos.FaltaCampo("Area");
            return false;
        } else if (this.ExistenciaArea() == true) {
            global.Mensajes.mostrarMensaje("Advertencia", "El area ya existe", 2);
            return false;
        } else if (Sql.insertar("INSERT INTO AREA VALUES (" + this.getMax() + ", '" + this.area + "')") == true) {
            //Dialogos.GuardadoconExito();
            this.limpiarArea();
            return true;
        } else {
            global.Mensajes.mostrarMensaje("Error", "Error al ingresar los datos...!", 3);
            return false;
        }
        
    }

    private Boolean ExistenciaArea() {
        sql = "SELECT * FROM AREA WHERE AREA='" + this.area + "'";
        return Sql.isExiste(sql);
    }

    
    public void listarArea(JComboBox cbo) {
        cbo.removeAllItems();
        cbo.addItem("--Seleccione--");
        sql = "SELECT AREA FROM AREA";
        Sql.getConsulta(sql, dt);
        while (dt.next()) {
            cbo.addItem(dt.getString(1));
        }
        cbo.setSelectedIndex(0);

    }

    private void limpiarArea() {
        area = "";
        idarea=0;

    }

    public void sacarIdArea(String area) {
        sql = "SELECT IDAREA FROM AREA WHERE AREA='" + area + "'";
        Sql.getConsulta(sql, dt);
        while (dt.next()) {
            this.idarea = dt.getInt(1);
        }
    }
}
